package com.my.blog.website.helper.base;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @Auther lxf
 * @Date 2021/7/3
 */
@Component
public class RedisCache {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Resource
    private ValueOperations<String, Object> valueOperations;

    public void set(String key, Object object) {
        valueOperations.set(key, object);
    }

    public <T> T get(String key) {
        return (T) valueOperations.get(key);
    }

    public void set(String key, Object o, int seconds) {
        valueOperations.set(key, o, seconds, TimeUnit.MILLISECONDS);
    }

    public void remove(String key) {
        redisTemplate.delete(key);
    }
}
